<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Shopeco_Model extends ThemeModel
{
	public function __construct() {
        parent::__construct();
	}

	public function countAllProduct($option = array()) {
        $this->db->from($this->table_product_name .         ' AS a');
        $this->db->join($this->table_product_details_name . ' b', 'a.id=b.product_id');
        $this->db->join($this->table_shop_name            . ' c', 'a.shop_id=c.id');
        $this->db->join($this->table_category_name        . ' d', 'a.cate_id=d.id');
        $this->db->join($this->table_user_name            . ' e', 'c.user_id=e.id');
        $this->db->join($this->table_shop_category_name   . ' f', 'a.shop_cate_id=f.id');
        $this->db->where('a.deleted_at', NULL);
        $this->db->where('b.is_avatar', ON);
        $this->db->where('c.deleted_at', null);
        $this->db->where('d.deleted_at', null);
        $this->db->where('e.deleted_at', null);
        $this->db->where('f.deleted_at', null);
        if (count($option) > 0) {
            $this->db->where($option);
        }
        return $this->db->count_all_results();
    }

	public function paging($limit, $offset, $option = array(), $select=array()) {
        if(count($select)>0){
            $select = implode(',', $select);
        } else {
            $select = "a.id AS product_id, a.title AS product_title, a.slug AS product_slug,
                        a.cate_id AS category_id, a.shop_id AS shop_id, a.shop_cate_id,
                        a.created_at AS product_created_at, a.active_flg AS product_active_flg, a.sale_flg AS product_sale_flg,
                        a.deleted_at AS product_deleted_at,
                    b.id as product_detail_id, b.code, b.image, b.price_new, b.price_old,b.is_avatar,
                    c.title AS shop_title,c.slug AS shop_slug,
                    d.title AS category_title, d.slug AS category_slug,
                    e.image_folder AS shop_image_folder,
                    f.title AS shop_cate_title, f.slug AS shop_cate_slug";
        }
        $result = null;
        $this->db->select($select, true);
        $this->db->from($this->table_product_name .         ' AS a');
        $this->db->join($this->table_product_details_name . ' b', 'a.id=b.product_id');
        $this->db->join($this->table_shop_name            . ' c', 'a.shop_id=c.id');
        $this->db->join($this->table_category_name        . ' d', 'a.cate_id=d.id');
        $this->db->join($this->table_user_name            . ' e', 'c.user_id=e.id');
        $this->db->join($this->table_shop_category_name   . ' f', 'a.shop_cate_id=f.id');
        $this->db->where('a.deleted_at', NULL);
        $this->db->where('b.is_avatar', ON);
        $this->db->where('c.deleted_at', null);
        $this->db->where('d.deleted_at', null);
        $this->db->where('e.deleted_at', null);
        $this->db->where('f.deleted_at', null);
        if (count($option) > 0) {
            $this->db->where($option);
        }
        $this->db->order_by('a.created_at', 'desc');
        $this->db->order_by('a.id', 'desc');
        $this->db->limit($limit, $offset);
        $result = $this->db->get()->result('Product_Details_Entity');

        return $result;
    }

    public function getAllShopCategory($shop_slug, $option = array(), $aSelect=array()) {
        $this->CI->load->model('entity/shop_category_entity', 'Shop_Category_Entity');
        $select = "a.id, a.title, a.slug, d.slug as parent_slug, d.title as parent_title";
        if(count($aSelect)>0){
            $select = implode(',', $aSelect);
        }
        $this->db->select($select, true);
        $this->db->from($this->table_shop_category_name . ' AS a');
        $this->db->join($this->table_shop_name . ' b', 'a.shop_id=b.id');
        $this->db->join($this->table_user_name . ' c', 'b.user_id=c.id');
        $this->db->join($this->table_category_name . ' d', 'a.cate_id=d.id');
        $this->db->where('b.slug', $shop_slug);
        $this->db->where('a.deleted_at', null);
        $this->db->where('b.deleted_at', null);
        $this->db->where('c.deleted_at', null);
        $this->db->where('d.deleted_at', null);
        $this->db->order_by('a.order', 'ASC');
        $this->db->order_by('a.created_at', 'DESC');
        if (count($option) > 0) {
            $this->db->where($option);
        }
        return $this->db->get()->result('Shop_Category_Entity');
    }
}
?>